package org.threegroup.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.threegroup.entity.Goods;
import org.threegroup.service.GoodsService;
import org.threegroup.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 留言页：如果有其他模块需要调用货物的信息，请在这里写下返回值、参数、方法名
 *
 */
@WebServlet("/goods")
public class GoodsController extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methods = req.getParameter("methods");
        System.out.println("Goods已获取----"+methods);
        switch (methods){
            case "queryAllGoods":
                req.setCharacterEncoding("application/json;charset=utf-8");
                List<Goods> goods = goodsService.queryAllGoods();
                resp.getWriter().write(goods!=null ? JSONUtil.parse(goods).toString():"暂无货物信息");
                break;
            case "queryGoodsName":
                req.setCharacterEncoding("application/json;charset=utf-8");
                String name = "%"+req.getParameter("name")+"%";
                List<Goods> goodsName = goodsService.queryGoodsName(name);
                resp.getWriter().write(goodsName!=null ? JSONUtil.parse(goodsName).toString():"暂无货物信息");
                break;
            case "":
        }
    }
}
